package main;

import java.awt.Button;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

public class NumberSelecter {
	private int number = 1;
	private int min = 1;
	private int max = 1;
	
	public Label valueLabel;
	public Label noteLabel;
	public Button increment;
	public Button decrement;
	
	public String note = "";
	
	public Button doneSelecting;
	public Button codeToExecute;
	public UI myUI;
	
	public NumberSelecter(int min, int max, int start, String note, UI myUI, Button codeToExecute) {
		this.min = min;
		this.max = max;
		this.number = start;
		this.note = note;
		this.myUI = myUI;
		this.codeToExecute = codeToExecute;
		this.makeButtons();
	}
	
	public NumberSelecter(int min, int max, String note, UI myUI, Button codeToExecute) {
		this.min = min;
		this.max = max;
		this.number = min;
		this.note = note;
		this.myUI = myUI;
		this.codeToExecute = codeToExecute;
		this.makeButtons();
	}
	
	public NumberSelecter(int max, String note, UI myUI, Button codeToExecute) {
		this.min = 1;
		this.max = max;
		this.number = 1;
		this.note = note;
		this.myUI = myUI;
		this.codeToExecute = codeToExecute;
		this.makeButtons();
	}
	
	public NumberSelecter(String note, UI myUI, Button codeToExecute) {
		this.min = 1;
		this.max = Integer.MAX_VALUE;
		this.number = 1;
		this.note = note;
		this.myUI = myUI;
		this.codeToExecute = codeToExecute;
		this.makeButtons();
	}
	
	
	public void makeButtons() {
		myUI.storeVisibleComponents();
		myUI.disableComponents();
		
		doneSelecting = new Button("Done selecting");
		doneSelecting.setBounds(myUI.dim.width-200, myUI.dim.height - 330, 100, 50);
		doneSelecting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Clean up
				myUI.remove(valueLabel);
				myUI.remove(noteLabel);
				myUI.remove(increment);
				myUI.remove(decrement);
				myUI.remove(doneSelecting);
				myUI.reenableComponents();
				if (codeToExecute != null)
					codeToExecute.dispatchEvent(new MouseEvent(codeToExecute, MouseEvent.MOUSE_CLICKED, 
							System.currentTimeMillis(), 0, 10, 10, 1, false));
			}
		});
		myUI.add(doneSelecting);
		
		valueLabel = new Label(number + " ");
		valueLabel.setBounds(myUI.dim.width - 150, myUI.dim.height - 400, 50, 20);
		
		noteLabel = new Label(note);
		noteLabel.setBounds(myUI.dim.width-170, myUI.dim.height - 430, 150, 20);
		
		increment = new Button("+");
		increment.setBounds(myUI.dim.width-100, myUI.dim.height - 400, 20, 20);
		increment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				decrement.setEnabled(true);
				number += 1;
				if (number >= max)
					increment.setEnabled(false);
				valueLabel.setText(number + " ");				
				myUI.repaint();
			}
		});
		
		decrement = new Button("-");
		decrement.setBounds(myUI.dim.width-170, myUI.dim.height - 400, 20, 20);
		decrement.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				increment.setEnabled(true);
				number -= 1;
				if (number <= min)
					decrement.setEnabled(false);
				valueLabel.setText(number + " ");
				myUI.repaint();
			}
		});
		
		myUI.add(valueLabel);
		myUI.add(noteLabel);
		myUI.add(increment);
		myUI.add(decrement);
	}
	
	
	public int getNumber() {
		return number;
	}
	public void setNumber(int n) {
		number = n;
	}

	public void setCodeToExecute(Button codeToExecute) {
		this.codeToExecute = codeToExecute;
	}
}
